home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1148 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. From: jah@cais.cais.com (John A Hughes)
  2. Message-ID: <4l0k0e$329@news2.cais.com>
  3. X-Original-Date: 16 Apr 1996 17:05:18 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 18 Apr 96 00:44:51 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: methods for accessing attributes
  9. Organization: Capital Area Internet Service info@cais.com 703-448-4470
  10. References: <9604160147.AA25931@grace>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMXWQ1uEDnX0m9pzZAQFPLQF8Du8J7REeSXZMxTESKpdJLnwyRMa74w72
  13.     UcprIMj7oIGQFX+FMl6IhUMdNUD0hKr+
  14.     =s/5R
  15.  
  16. In article <9604160147.AA25931@grace>,
  17. Steve Clamage <clamage@taumet.eng.sun.com> wrote:
  18. [I don't think this was really Steve Clamage, but that's what my 
  19. newsreader said!!]
  20. >If lots of code already exists like the following:
  21. >
  22. >    void blah(Display &d)
  23. >    {
  24. >        d.wid = 80;
  25. >    }
  26. >
  27. >And now suppose that we want Display to do something when
  28. >the width is changed--- has there been any consideration of adding
  29. >a way of doing this?  (besides changing all the direct accesses
  30. >to function calls)   
  31. >Or is there some way, within the current standard, to do this?
  32. >I toyed with some ways that involved replacing the type of 'wid'
  33. >(int) with some class that did the desired operation when assigned to,
  34. >but in order to have full access to Display, it has to be a specialized
  35. >class for each instance variable, and each class has to have special
  36. >code containing knowledge of the offset of 'wid' within 'Display'
  37. >and then it has to use that offset along with some really evil
  38. >casting to find the address of the 'Display' object it resides in.
  39.  
  40. No, not necessarily. For the exact case you showed in your post, you
  41. could have a class like this, though it wouldn't solve all your 
  42. problems:
  43.  
  44. template<class MainClass, class MemberClass>
  45. class SmartMember {
  46. public:
  47.    SmartMember(MainClass &o, void(*MainClass::f)(MemberClass)) 
  48.       : main_obj(o), change_fn(f) {}
  49.    SmartMember &operator=(MemberClass t) {
  50.       main_obj.change_fn(t); 
  51.    }
  52.    operator T() {return datum;}
  53. private:
  54.    MainClass &main_obj;
  55.    MemberClass datum;
  56.    void(*MainClass::change_fn)(MemberClass);
  57. };
  58.  
  59. So now your class Display looks something like this:
  60.  
  61. class Display {
  62. public:
  63.    Display(...);
  64.    SmartMember<Display,int> wid;
  65. //...
  66. private:
  67.    void updateWid(int new_wid);
  68. //...
  69. };
  70.  
  71. and you'll need to make sure your Display constructors do the following:
  72.  
  73. Display::Display(...) : wid(*this, &Display::updateWid) {
  74. //...
  75. }
  76.  
  77. which means that wid now knows it's a member of the given display and that
  78. when it changes it should change via updateWid.
  79.  
  80. Now you can leave all your 
  81.  
  82. void blah(Display &d)
  83. {
  84.     d.wid = 80;
  85. }
  86.  
  87. as they are. The SmartMember class is generic so you can do it for
  88. every kind of member of every kind of class that has assignment
  89. semantics.
  90.  
  91. I suspect defining this one class and changing a couple constructors
  92. is easier than changing all your assignments, but then again, it's
  93. weird and I only suggested it to show that you COULD do it without
  94. changing all your assignment statements or defining a class for every
  95. type.  This won't work at all if you ever pass wid by reference and
  96. expect changes to happen that way. You should have known better than
  97. to make your member data public in the first place, and rewriting it
  98. is probably the RIGHT thing to do. 
  99.  
  100.  
  101. Have fun
  102.  
  103. jah
  104. ---
  105. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  106. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  107. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  108. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  109. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  110.